#include "stdafx.h"
#pragma warning (disable : 4786)
#pragma warning (disable : 4086)

#include "ClientCallbackGame.h"
ClientCallbackGame::ClientCallbackGame()
{
	m_pSocketClient = NULL;
	RegisterHandler(0,boost::bind(&ClientCallbackGame::phPing_0, this,_1));
}
ClientCallbackGame::ClientCallbackGame(ISocketClient *pSocketClient)
{
	m_pSocketClient = pSocketClient;
}

void ClientCallbackGame::OnConnect()
{
}
void ClientCallbackGame::OnReconnect()
{
}

void ClientCallbackGame::OnRecvData(const void* pData, size_t nDataLen)
{
	PacketDecoder decoder;
	if(!decoder.SetData(reinterpret_cast<const char*>(pData), nDataLen + 4))
	{
		LOGGER_ERROR("!decoder.SetData(reinterpret_cast<const char*>(pData), nDataLen + 4)");
		return;
	}

	if(decoder.GetItemCount() < 3)
	{
		LOGGER_ERROR("decoder.GetItemCount() < 3");
		return;
	}

	Int32 nSocketIdOnGate = 0;
	Int32 type = 0;
	Int32 id = 0;
	Int32 nIndex = 1;
	std::list<Int32> listSocketId;
	//if(!decoder.GetItem(0, nSocketIdOnGate))
	if(!DecodeData(&decoder,nIndex,listSocketId))
	{
		LOGGER_ERROR("!DecodeData(&decoder,nIndex,listSocketId)");
		return;
	}
	//LOGGER_INFO("listSocketId.size():%d",listSocketId.size());

	if(!decoder.GetItem(nIndex++, type))
	{
		LOGGER_ERROR("!decoder.GetItem(nIndex++, type)");
		return;
	}

	if(!decoder.GetItem(nIndex++, id))
	{
		LOGGER_ERROR("!decoder.GetItem(nIndex++, id)");
		return;
	}

#ifdef _WIN32
	if(!(type == 0 && id == 0))
		printf("ClientCallbackGame OnRecvData nSocketIdOnGate:%d,type:%d,id:%d\n",nSocketIdOnGate,type,id);
#endif

	if(id < 0 || id >= 65536)
	{
		LOGGER_ERROR("id < 0 || id >= 65536,id:%d",id);
		return;
	}

	PacketHandler method = GetHandler(id);
	if(method != NULL)
	{
		method((void*)(&decoder));
	}

	PacketEncoder encoder;
	EncodeData(&encoder,decoder,listSocketId.size()+1);
	if(listSocketId.size() == 0)
	{
		singleton<ServerCallbackGate>::instance().SendPacketToAll(encoder);
	}
	else
	{
		std::list<Int32>::iterator iter = listSocketId.begin();
		std::list<Int32>::iterator iterend = listSocketId.end();
		for (; iter != iterend; iter++)
		{
			singleton<ServerCallbackGate>::instance().SendPacket(*iter,encoder);
		}
	}
};
void ClientCallbackGame::phPing_0(void *pDecoder)
{
	static PacketEncoder encoder;
	encoder.Clear();
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(0));
	encoder.AddItem(Int32(0));
	SendPacket(encoder);
}
void ClientCallbackGame::SendPacket(PacketEncoder& encoder)
{
	if(encoder.GetDataSize() == 0)
	{
		LOGGER_ERROR("SendPacket,encoder.GetDataSize() == 0");
		return;
	}
	void   *p = const_cast<void*>(encoder.GetData());
	m_pSocketClient->SendData((char*)p+sizeof(UInt32),encoder.GetDataSize()-sizeof(UInt32));
}
void ClientCallbackGame::OnCloseConnection()
{
};
void ClientCallbackGame::OnUpdate()
{
}


// ISocketClientCallback* CreateClientCallbackData(ISocketClient*pSocketClient)
//{
//	singleton<ClientCallbackGame>::instance().setSocketClient(pSocketClient);
//	return &(singleton<ClientCallbackGame>::instance());
//}